package com.bckj.fastboot.iot.mq.redis.producer;

import com.bckj.fastboot.core.jackson.JsonUtils;
import com.bckj.fastboot.core.redis.RedisOps;
import com.bckj.fastboot.iot.common.constant.IotMQConstants;
import com.bckj.fastboot.iot.common.dto.msg.IotAlarmMessage;
import com.bckj.fastboot.iot.common.mq.producer.IotAlarmMessageProducer;
import com.bckj.fastboot.iot.mq.redis.delaymsg.DelayedMessageSender;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * AlarmInfoMessageProducerImpl
 *
 * @author wfc
 **/
@Component
public class IotAlarmMessageProducerImpl implements IotAlarmMessageProducer {

    @Resource
    private RedisOps redisOps;
    @Resource
    private DelayedMessageSender delayedMessageSender;


    @Override
    public void send(IotAlarmMessage message) {
        redisOps.llPush(IotMQConstants.IOT_ALARM_INFO_TOPIC, message);
    }

    @Async
    @Override
    public void sendAsync(IotAlarmMessage message) {
        send(message);
    }

    @Override
    public void sendDelay(IotAlarmMessage message, long delay) {
        delayedMessageSender.sendDelayedMessage(
                IotMQConstants.IOT_ALARM_INFO_TOPIC,
                JsonUtils.toJsonStr(message),
                delay,
                TimeUnit.MILLISECONDS
        );
    }
}
